"""按厂家进行库存统计和评库"""

from odoo import models, api, fields
from odoo.tools import float_compare
from odoo.exceptions import UserError, ValidationError


class Quant(models.Model):
    _inherit = 'stock.quant'

    producer_id = fields.Many2one('gsp.producer', string="生产厂家", store=True,
                                  related="product_id.producer_id")


class Inventory(models.Model):
    _inherit = 'stock.inventory'

    @api.model
    def _selection_filter(self):
        """ 评库表上添加生产厂家的选项"""
        res_filter = super()._selection_filter()
        res_filter.append(('producer', '生产厂家'))
        return res_filter

    producer_id = fields.Many2one('gsp.producer', string="生产厂家")

    @api.model
    def _get_inventory_lines_values(self):
        self.ensure_one()
        vals = []
        product_obj = self.env['product.product']
        if self.filter == 'producer' and self.producer_id:
            products = product_obj.search([('producer_id', 'in', self.producer_id.ids)])
            # 因吹斯听！
            inventory = self.new(self._convert_to_write(self.read()[0]))
            inventory.filter = 'producer'
            for product in products:
                inventory.product_id = product
                vals += super(Inventory, inventory)._get_inventory_lines_values()
        else:
            vals = super()._get_inventory_lines_values()

        return vals
